package com.example.apple.hxbtc.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.apple.hxbtc.R;

/**
 * Created by hpz on 17/2/22.
 */

public class BaseActivity extends FragmentActivity implements View.OnClickListener {

    private LinearLayout mLayout;
    private RelativeLayout mTitleBar;
    private Button mBack;
    private Button mMenu;
    private TextView mTitle;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);

        initView();
    }

    private void initView() {
        mLayout = (LinearLayout) super.findViewById(R.id.content);
        mTitleBar = (RelativeLayout) super.findViewById(R.id.title_bar);
        mBack = (Button) super.findViewById(R.id.back);
        mTitle = (TextView) super.findViewById(R.id.title);
        mMenu = (Button) super.findViewById(R.id.right_button);

        mBack.setOnClickListener(this);
        mMenu.setOnClickListener(this);
    }



    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public void setTitle(int resId) {
        mTitle.setText(resId);
    }

    public void setTitleColor(int color) {
        mTitle.setTextColor(color);
    }

    public void setTitleColor(String color) {
        mTitle.setTextColor(Color.parseColor(color));
    }

    @SuppressLint("NewApi")
    public void setTitleBarBg(Drawable background) {
        mTitleBar.setBackground(background);
    }

    public void setTitleBarBgColor(int color) {
        mTitleBar.setBackgroundColor(color);
    }

    public void setTitleBarBg(int resid) {
        mTitleBar.setBackgroundResource(resid);
    }

    public void setTitleBarVisibility(int visibility) {
        mTitleBar.setVisibility(visibility);
    }

    public void setTitleVisibility(int visibility) {
        mTitle.setVisibility(visibility);
    }

    public void setMenuVisibility(int visibility) {
        mMenu.setVisibility(visibility);
    }

    public void setBackVisibility(int visibility) {
        mBack.setVisibility(visibility);
    }

    public void setContentView(int layoutResID) {
        LayoutInflater.from(this).inflate(layoutResID, mLayout, true);
    }

    public void setContentView(View view) {
        mLayout.addView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    public View findViewById(int id) {
        return mLayout.findViewById(id);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        }
        //if (id == R.id.menu) {
        //    if (titleBarListener != null) {
        //        titleBarListener.menuCilck(v);
         //   }
        //}
    }

    public interface TitleBarListener {
        public void menuCilck(View v);
    }

    private TitleBarListener titleBarListener;

    public void setTitleBarListener(TitleBarListener titleBarListener) {
        mMenu.setVisibility(View.VISIBLE);
        this.titleBarListener = titleBarListener;
    }

    protected void mToast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    protected void mToast(int resId){
        mToast(getString(resId));
    }

    protected Button getRightButton(){
        mMenu.setVisibility(View.VISIBLE);
        return mMenu;
    }
    protected Button getBackButton(){
        return mBack;
    }

    protected LinearLayout getLinearLayout(){
        linearLayout.setVisibility(View.VISIBLE);
        return linearLayout;
    }

    /*public void showDialog(String content,View.OnClickListener positiveListener){
        showDialog(null, content, null, null, positiveListener, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消按钮
            }
        });
    }*/

    /*
    public void showDialog(String title,String content,String positiveText,String negativeText,View.OnClickListener positiveListener,View.OnClickListener negativeListener){
        MyAlertDialog dialog = new MyAlertDialog(this).builder();
        if (TextUtils.isEmpty(title)){
            title = getString(R.string.dialog_tips);
        }
        dialog.setTitle(title);
        dialog.setMsg(content==null?"":content);

        if (TextUtils.isEmpty(positiveText)){
            positiveText = getString(R.string.dialog_confirm);
        }
        if (TextUtils.isEmpty(negativeText)){
            negativeText = getString(R.string.dialog_cancel);
        }
        dialog.setPositiveButton(positiveText,positiveListener);
        dialog.setNegativeButton(negativeText,negativeListener);
        dialog.show();
    }*/


    public Context getContext(){
        return this;
    }

    /**
     * 隐藏键盘
     */
    public void hideKeyBoard(){
        InputMethodManager inputManger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManger.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    public int dp2px(float dp){
        float scale = getResources().getDisplayMetrics().density;
        return (int) (scale*dp+0.5f);
    }
}
